package com.wust.supermarket.service.impl;

import com.wust.supermarket.dao.InstockDao;
import com.wust.supermarket.dao.MerchDao;
import com.wust.supermarket.dao.ProviderDao;
import com.wust.supermarket.domain.Instock;
import com.wust.supermarket.domain.Merch;
import com.wust.supermarket.domain.Provider;
import com.wust.supermarket.service.InstockService;
import com.wust.supermarket.utils.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InstockServiceImpl implements InstockService {
    @Resource
    private MerchDao merchDao;
    @Resource
    private InstockDao instockDao;
    @Resource
    private ProviderDao providerDao;

    /*@Override
    @Transactional
    *//**
     * 徐险nb版
     *//*
    public boolean insertInstock(Instock instock) {
        merch merch1 = instock.getMerch();
        int merch_id = merch1.getMerchId();
        Provider provider1 = instock.getProvider();
        Provider provider2 = providerDao.getProviderById(provider1.getProviderId());
        if (provider2 == null) {
            int p = providerDao.insertProvider(provider1);
            if (p <= 0)
                return false;
        }
        //由于自增主键，返回自增的主键值，重新再导入新主键
        instock.setProvider(provider1);
        merch merch2 = merchDao.getMerchById(merch_id);
        if (merch2 == null) {
            int i = merchDao.insertMerch(merch1);
            if (i > 0) {
                //由于自增主键，返回自增的主键值，重新再导入新主键
                instock.setMerch(merch1);
                //增加instock的ID
                instock.setInstockId(TimeUtil.InstockInitialization());
                int j = instockDao.insertInstock(instock);
                if (j <= 0)
                    return false;
            } else
                return false;
        } else {
            int num = merch2.getMerchNumber() + instock.getNumber();
            merch2.setMerchNumber(num);
            int i = merchDao.updateMerch(merch2);
            if (i > 0) {
                //增加instock的ID
                instock.setInstockId(TimeUtil.InstockInitialization());
                int j = instockDao.insertInstock(instock);
                if (j <= 0)
                    return false;
            } else
                return false;
        }
        return true;
    }*/


    @Override
    @Transactional
    public boolean insertInstock(Instock instock) {
        Merch merch = instock.getMerch();
        Provider provider = instock.getProvider();
        if (merchDao.getMerchById(merch.getMerchId()) == null)
            return false;
        if (providerDao.getProviderById(provider.getProviderId()) == null)
            return false;
        instock.setInstockId(TimeUtil.InstockInitialization());
        if (instockDao.insertInstock(instock) == 0)
            return false;
        return true;
    }

    @Override
    public boolean updateInstock(Instock instock) {
        return instockDao.updateInstock(instock) > 0;
    }

    @Override
    public boolean deleteInstockById(String instockId) {
        return instockDao.deleteInstockById(instockId) > 0;
    }

    @Override
    public boolean deleteInstockByMerchId(int merchId) {
        return instockDao.deleteInstockByMerchId(merchId) > 0;
    }

    @Override
    public boolean deleteInstockByProviderId(int providerId) {
        return instockDao.deleteInstockByProviderId(providerId) > 0;
    }

    @Override
    public List<Instock> listInstocks() {
        return instockDao.listAllInstocks();
    }

    @Override
    public Instock getInstockById(String instockId) {
        return instockDao.getInstockById(instockId);
    }

    @Override
    public List<Instock> listInstockByMerchId(int merchId) {
        return instockDao.listInstockByMerchId(merchId);
    }

    @Override
    public List<Instock> listInstockByProviderId(int providerId) {
        return instockDao.listInstockByProviderId(providerId);
    }
}
